package graphisme;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.DefaultCaret;

import player.Player;

public class ChatGrapicalnterface extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	public Map<String, Integer> adrOpoppent = null;
	private String ENTER = "Enter";
	JButton enterButton;
	public JTextArea output;
	public JTextField input;
	public JFrame frame;
	public JPanel panel;
	public Player player = null;

	public ChatGrapicalnterface(Player player) {
		this.player = player;
	}

	public void setVisible(boolean b) {
		frame.setVisible(b);
	}

	public void setAdrOpoppent(Map<String, Integer> hash) {
		this.adrOpoppent = hash;
	}

	public void createFrame() {
		this.adrOpoppent = new HashMap<String, Integer>();
		frame = new JFrame("Chat");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.setOpaque(true);
		ButtonListener buttonListener = new ButtonListener();
		output = new JTextArea(15, 50);
		output.setWrapStyleWord(true);
		output.setEditable(false);
		JScrollPane scroller = new JScrollPane(output);
		scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		JPanel inputpanel = new JPanel();
		inputpanel.setLayout(new FlowLayout());
		input = new JTextField(20);
		enterButton = new JButton("Enter");
		enterButton.setActionCommand(ENTER);
		enterButton.addActionListener(buttonListener);
		// enterButton.setEnabled(false);
		input.setActionCommand(ENTER);
		input.addActionListener(buttonListener);
		DefaultCaret caret = (DefaultCaret) output.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		panel.add(scroller);
		inputpanel.add(input);
		inputpanel.add(enterButton);
		panel.add(inputpanel);
		frame.getContentPane().add(BorderLayout.CENTER, panel);
		frame.pack();
		frame.setLocationByPlatform(true);
		// Center of screen
		// frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		input.requestFocus();
	}

	// method handling action on buttons
	public class ButtonListener implements ActionListener {

		public void actionPerformed(final ActionEvent ev) {
			// if text is not empty
			if (!input.getText().trim().equals("")) {
				// if player clicks on button send
				String cmd = ev.getActionCommand();
				if (ENTER.equals(cmd)) {
					// add pseudo and text to the window and send
					output.append(player.getPseudo() + " : " + input.getText()
							+ "\n");
					try {
						// create datagram for the player
						DatagramSocket clientSocket = new DatagramSocket();
						byte[] sendData = new byte[1024];
						// send datagram packet to every player of the game
						for (Entry<String, Integer> entry : getAdrOpoppent()
								.entrySet()) {
							String toSend = player.getPseudo() + " : "
									+ input.getText();
							sendData = toSend.getBytes();
							DatagramPacket sendPacket = new DatagramPacket(
									sendData, sendData.length,
									InetAddress.getByName("localhost"),
									entry.getValue());
							clientSocket.send(sendPacket);
							// close socket
							clientSocket.close();
						}

					} catch (IOException e) {
						System.out
								.println("Problème while chatting");
						e.printStackTrace();
					}
				}
			}
			// reset text
			input.setText("");
			input.requestFocus();
		}
	}

	public Map<String, Integer> getAdrOpoppent() {
		return adrOpoppent;
	}

	// inherited method
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}
}
